package com.example.erat.repository;

import com.example.erat.entity.Experiment;
import com.example.erat.entity.ExperimentSubmission;
import com.example.erat.entity.Student;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Query;
import org.springframework.stereotype.Repository;

import java.util.List;

/**
 * 实验提交记录数据访问层
 */
@Repository
public interface ExperimentSubmissionRepository extends JpaRepository<ExperimentSubmission, Long> {
    
    /**
     * 根据学生和实验查询提交记录
     */
    ExperimentSubmission findByStudentAndExperiment(Student student, Experiment experiment);
    
    /**
     * 根据学生ID和实验ID查询提交记录
     */
    ExperimentSubmission findByStudentIdAndExperimentId(Long studentId, Long experimentId);
    
    /**
     * 根据实验查询提交记录列表
     */
    List<ExperimentSubmission> findByExperiment(Experiment experiment);
    
    /**
     * 根据实验ID查询提交记录列表
     */
    List<ExperimentSubmission> findByExperimentId(Long experimentId);
    
    /**
     * 根据学生查询提交记录列表
     */
    List<ExperimentSubmission> findByStudent(Student student);
    
    /**
     * 根据学生ID查询提交记录列表
     */
    List<ExperimentSubmission> findByStudentId(Long studentId);
    
    /**
     * 根据实验ID查询已提交的学生ID列表
     */
    @Query("SELECT es.student.id FROM ExperimentSubmission es WHERE es.experiment.id = ?1 AND es.status = 'SUBMITTED'")
    List<Long> findSubmittedStudentIdsByExperimentId(Long experimentId);
    
    /**
     * 根据学生ID查询已提交的实验ID列表
     */
    @Query("SELECT es.experiment.id FROM ExperimentSubmission es WHERE es.student.id = ?1 AND es.status = 'SUBMITTED'")
    List<Long> findSubmittedExperimentIdsByStudentId(Long studentId);
} 